<?php
// 首页显示信息以及基本配置
namespace Partner\Controller;
use Think\Controller;
class CommonController extends Controller {
    function _initialize(){
        if( !session('user_id') ){
            $this->assign('title','用户登录');
            $this->display('User:login');
            die();
        }

        $menu   =   D('Menu');
        $menuArray  =   $menu->where('bclassid = 0 AND type = 1 AND ishidden = 0')->order('sort ASC')->select();
        if ($menuArray ){
            foreach ($menuArray AS $key=>$row ){
                $menuChildrenArray  =   array();
                $menuChildrenArray  =   $menu->where(' bclassid = '.$row['id'].' AND type = 1 AND ishidden = 0')->order('sort ASC')->select();
                $menuArray[$key]['sub'] =   $menuChildrenArray;
            }
        }
        $this->assign('menuArray',$menuArray);

        //验证是否有权限查看当前栏目
        $c_name = CONTROLLER_NAME;
        $a_name =  ACTION_NAME;
        $c_name=strtolower($c_name);
        $a_name=strtolower($a_name);
        $p_id=session('id');
        $user_info=M('partner')->find($p_id);
        if($user_info['partent_id']!=0){
            $rbac_ids=M('rbac')->where('type=2 and uid='.$p_id)->find();
            if($rbac_ids){
                $menus  =   $menu->where('type = 1')->order('sort ASC')->select();
                foreach ($menus as $key=>$val){
                    $module=strtolower($val['module']);
                    $action=strtolower($val['action']);
                    if($c_name==$module&&$action==$a_name){
                        $rbac_id=$val['id'];
                    }
                }
                if($rbac_id){
                    $bids=explode(',',$rbac_ids['bids']);
                    if(!in_array($rbac_id,$bids)){
                        $this->error('当前帐号未分配权限','/index/index_info');
                    }
                }
            }else{
                if($c_name!='index'||$a_name!='index'){
                    $this->error('当前帐号未分配权限','/index/index_info');
                }
            }
        }

    }
    public function ajax_r($status='0',$message='',$data=array()) {
        $msg['status']=$status;
        $msg['data']=$data;
        $msg['message']=$message;
        $this->ajaxReturn($msg);
    }

}